package com.dragon.mmochat.model.serverListDao.job;

import java.io.File;
import java.io.FileWriter;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.quartz.Job;
import org.quartz.JobExecutionContext;

import com.dragon.mmochat.MmochatConstant;
import com.dragon.mmochat.MmochatMain;
import com.dragon.mmochat.model.serverListStatistics.MmochatPayRecord;
import com.dragon.mmochat.service.ServerListStatistics;

//统计新增注册skyId数
public class calNewRegSkyIdJob implements Job {
	@Override
	public void execute(JobExecutionContext context) {
		try {
			long curTick = System.currentTimeMillis();
			List<Long> list = new ArrayList<Long>();

			Timestamp start = new Timestamp(curTick
					- MmochatConstant.MS_PER_DAY);
			Timestamp end = new Timestamp(curTick);
			// 从所有区读取当天的新增用户数
			list.clear();
			try {
				List<Long> list1 = MmochatMain.serverListDaoService
						.mmochat1_getSkyIdByDate(start, end);
				if (list1 != null) {
					list.addAll(list1);
					list1.clear();
				}
				Thread.sleep(500);
				list1 = MmochatMain.serverListDaoService
						.mmochat2_getSkyIdByDate(start, end);
				if (list1 != null) {
					list.addAll(list1);
					list1.clear();
				}
				Thread.sleep(500);
				list1 = MmochatMain.serverListDaoService
						.mmochat3_getSkyIdByDate(start, end);
				if (list1 != null) {
					list.addAll(list1);
					list1.clear();
				}
				Thread.sleep(500);
				list1 = MmochatMain.serverListDaoService
						.mmochat4_getSkyIdByDate(start, end);
				if (list1 != null) {
					list.addAll(list1);
					list1.clear();
				}
				Thread.sleep(500);
				list1 = MmochatMain.serverListDaoService
						.mmochat6_getSkyIdByDate(start, end);
				if (list1 != null) {
					list.addAll(list1);
					list1.clear();
				}
				Thread.sleep(500);
				list1 = MmochatMain.serverListDaoService
						.mmochat7_getSkyIdByDate(start, end);
				if (list1 != null) {
					list.addAll(list1);
					list1.clear();
				}
				Thread.sleep(500);
				list1 = MmochatMain.serverListDaoService
						.mmochat8_getSkyIdByDate(start, end);
				if (list1 != null) {
					list.addAll(list1);
					list1.clear();
				}
				Thread.sleep(500);
				list1 = MmochatMain.serverListDaoService
						.mmochat9_getSkyIdByDate(start, end);
				if (list1 != null) {
					list.addAll(list1);
					list1.clear();
				}
				Thread.sleep(500);
				list1 = MmochatMain.serverListDaoService
						.mmochat10_getSkyIdByDate(start, end);
				if (list1 != null) {
					list.addAll(list1);
					list1.clear();
				}
				Thread.sleep(500);
				list1 = MmochatMain.serverListDaoService
						.mmochat11_getSkyIdByDate(start, end);
				if (list1 != null) {
					list.addAll(list1);
					list1.clear();
				}
				Thread.sleep(500);
				list1 = MmochatMain.serverListDaoService
						.mmochat12_getSkyIdByDate(start, end);
				if (list1 != null) {
					list.addAll(list1);
					list1.clear();
				}
				Thread.sleep(500);
				list1 = MmochatMain.serverListDaoService
						.mmochat13_getSkyIdByDate(start, end);
				if (list1 != null) {
					list.addAll(list1);
					list1.clear();
				}
				Thread.sleep(500);
			} catch (Exception e) {
				e.printStackTrace();
				list.clear();
				System.out.println("mmochat_getSkyIdByDate等读取失败!");
			}

			int sum = 0;
			for (Long skyId : list) {
				if (!ServerListStatistics.skyIdMap.containsKey(skyId)) {
					sum++;
					ServerListStatistics.skyIdMap.put(skyId, (byte) 0);
				}
			}

			// 更新文件
			File oldFile = new File("skyIdMap");
			if (oldFile.exists() && oldFile.isFile()) {
				oldFile.delete();
			}
			FileWriter writer = new FileWriter("skyIdMap");
			if (writer != null) {
				for (Long skyid : ServerListStatistics.skyIdMap.keySet()) {
					writer.write(skyid + ",");
				}
				writer.close();
			}
			System.out.println("新增skyId查询完成，增加skyId数:" + sum + ", 耗时:"
					+ (System.currentTimeMillis() - curTick) + "ms");

			// 计算总登陆skyId数，平均登陆次数
			int totalLoginSkyIdNum = ServerListStatistics.todayLoginSkyIdMap
					.size();
			int totalLoginCount = 0;
			for (Integer v : ServerListStatistics.todayLoginSkyIdMap.values()) {
				if (v != null) {
					totalLoginCount += v;
				}
			}
			int average = 0;
			if (totalLoginSkyIdNum > 0) {
				average = 100 * totalLoginCount / totalLoginSkyIdNum;
			}

			ServerListStatistics.todayLoginSkyIdMap.clear();

			// 计算卡密兑换总额、skyId数、订单数
			long testTick = System.currentTimeMillis();
			long startTick = start.getTime(), endTick = end.getTime();
			List<MmochatPayRecord> payRecords = new ArrayList<MmochatPayRecord>();
			List<MmochatPayRecord> list2;
			try {
				list2 = MmochatMain.serverListDaoService
						.mmochat1_getDirectPayRecord(startTick, endTick);
				if (list2 != null) {
					payRecords.addAll(list2);
					list2.clear();
				}
				Thread.sleep(500);
				list2 = MmochatMain.serverListDaoService
						.mmochat2_getDirectPayRecord(startTick, endTick);
				if (list2 != null) {
					payRecords.addAll(list2);
					list2.clear();
				}
				Thread.sleep(500);
				list2 = MmochatMain.serverListDaoService
						.mmochat3_getDirectPayRecord(startTick, endTick);
				if (list2 != null) {
					payRecords.addAll(list2);
					list2.clear();
				}
				Thread.sleep(500);
				list2 = MmochatMain.serverListDaoService
						.mmochat4_getDirectPayRecord(startTick, endTick);
				if (list2 != null) {
					payRecords.addAll(list2);
					list2.clear();
				}
				Thread.sleep(500);
				list2 = MmochatMain.serverListDaoService
						.mmochat5_getDirectPayRecord(startTick, endTick);
				if (list2 != null) {
					payRecords.addAll(list2);
					list2.clear();
				}
				Thread.sleep(500);
				list2 = MmochatMain.serverListDaoService
						.mmochat6_getDirectPayRecord(startTick, endTick);
				if (list2 != null) {
					payRecords.addAll(list2);
					list2.clear();
				}
				Thread.sleep(500);
				list2 = MmochatMain.serverListDaoService
						.mmochat7_getDirectPayRecord(startTick, endTick);
				if (list2 != null) {
					payRecords.addAll(list2);
					list2.clear();
				}
				Thread.sleep(500);
				list2 = MmochatMain.serverListDaoService
						.mmochat8_getDirectPayRecord(startTick, endTick);
				if (list2 != null) {
					payRecords.addAll(list2);
					list2.clear();
				}
				Thread.sleep(500);
				list2 = MmochatMain.serverListDaoService
						.mmochat9_getDirectPayRecord(startTick, endTick);
				if (list2 != null) {
					payRecords.addAll(list2);
					list2.clear();
				}
				Thread.sleep(500);
				list2 = MmochatMain.serverListDaoService
						.mmochat10_getDirectPayRecord(startTick, endTick);
				if (list2 != null) {
					payRecords.addAll(list2);
					list2.clear();
				}
				Thread.sleep(500);
				list2 = MmochatMain.serverListDaoService
						.mmochat11_getDirectPayRecord(startTick, endTick);
				if (list2 != null) {
					payRecords.addAll(list2);
					list2.clear();
				}
				Thread.sleep(500);
				list2 = MmochatMain.serverListDaoService
						.mmochat12_getDirectPayRecord(startTick, endTick);
				if (list2 != null) {
					payRecords.addAll(list2);
					list2.clear();
				}
				Thread.sleep(500);
				list2 = MmochatMain.serverListDaoService
						.mmochat13_getDirectPayRecord(startTick, endTick);
				if (list2 != null) {
					payRecords.addAll(list2);
					list2.clear();
				}
				Thread.sleep(500);
			} catch (Exception e) {
				e.printStackTrace();
				payRecords.clear();
				System.out.println("mmochat_getDirectPayRecord等读取失败!");
			}
			int totalDirectPayRmb = 0; // 总卡密充值额
			Map<Long, Integer> directPayMap = new HashMap<Long, Integer>();
			for (MmochatPayRecord record : payRecords) {
				if (record != null) {
					directPayMap.put(record.getSkyId(), 0);
					totalDirectPayRmb += record.getRmb();
				}
			}
			int directPaySkyIdNum = directPayMap.size(); // 卡密充值人数
			int directPayTimes = payRecords.size(); // 卡密充值次数

			// 计算K币兑换总额、skyId数、订单数
			payRecords.clear();
			try {
				list2 = MmochatMain.serverListDaoService
						.mmochat1_getKBPayRecord(startTick, endTick);
				if (list2 != null) {
					payRecords.addAll(list2);
					list2.clear();
				}
				Thread.sleep(500);
				list2 = MmochatMain.serverListDaoService
						.mmochat2_getKBPayRecord(startTick, endTick);
				if (list2 != null) {
					payRecords.addAll(list2);
					list2.clear();
				}
				Thread.sleep(500);
				list2 = MmochatMain.serverListDaoService
						.mmochat3_getKBPayRecord(startTick, endTick);
				if (list2 != null) {
					payRecords.addAll(list2);
					list2.clear();
				}
				Thread.sleep(500);
				list2 = MmochatMain.serverListDaoService
						.mmochat4_getKBPayRecord(startTick, endTick);
				if (list2 != null) {
					payRecords.addAll(list2);
					list2.clear();
				}
				Thread.sleep(500);
				list2 = MmochatMain.serverListDaoService
						.mmochat5_getKBPayRecord(startTick, endTick);
				if (list2 != null) {
					payRecords.addAll(list2);
					list2.clear();
				}
				Thread.sleep(500);
				list2 = MmochatMain.serverListDaoService
						.mmochat6_getKBPayRecord(startTick, endTick);
				if (list2 != null) {
					payRecords.addAll(list2);
					list2.clear();
				}
				Thread.sleep(500);
				list2 = MmochatMain.serverListDaoService
						.mmochat7_getKBPayRecord(startTick, endTick);
				if (list2 != null) {
					payRecords.addAll(list2);
					list2.clear();
				}
				Thread.sleep(500);
				list2 = MmochatMain.serverListDaoService
						.mmochat8_getKBPayRecord(startTick, endTick);
				if (list2 != null) {
					payRecords.addAll(list2);
					list2.clear();
				}
				Thread.sleep(500);
				list2 = MmochatMain.serverListDaoService
						.mmochat9_getKBPayRecord(startTick, endTick);
				if (list2 != null) {
					payRecords.addAll(list2);
					list2.clear();
				}
				Thread.sleep(500);
				list2 = MmochatMain.serverListDaoService
						.mmochat10_getKBPayRecord(startTick, endTick);
				if (list2 != null) {
					payRecords.addAll(list2);
					list2.clear();
				}
				Thread.sleep(500);
				list2 = MmochatMain.serverListDaoService
						.mmochat11_getKBPayRecord(startTick, endTick);
				if (list2 != null) {
					payRecords.addAll(list2);
					list2.clear();
				}
				Thread.sleep(500);
				list2 = MmochatMain.serverListDaoService
						.mmochat12_getKBPayRecord(startTick, endTick);
				if (list2 != null) {
					payRecords.addAll(list2);
					list2.clear();
				}
				Thread.sleep(500);
				list2 = MmochatMain.serverListDaoService
						.mmochat13_getKBPayRecord(startTick, endTick);
				if (list2 != null) {
					payRecords.addAll(list2);
					list2.clear();
				}
				Thread.sleep(500);
			} catch (Exception e) {
				e.printStackTrace();
				payRecords.clear();
				System.out.println("mmochat_getKBPayRecord等读取失败!");
			}
			int totalKBPayRmb = 0; // 总K币充值额
			Map<Long, Integer> KBPayMap = new HashMap<Long, Integer>();
			for (MmochatPayRecord record : payRecords) {
				if (record != null) {
					KBPayMap.put(record.getSkyId(), 0);
					totalKBPayRmb += record.getRmb();
				}
			}
			int KBPaySkyIdNum = KBPayMap.size(); // K币充值人数
			int KBPayTimes = payRecords.size(); // K币充值次数

			for (Long skyId : KBPayMap.keySet()) {
				directPayMap.put(skyId, 0);
			}
			int mixPaySkyIdNum = directPayMap.size(); // 卡密+K币充值的skyId数

			System.out.println("充值记录查询完成，耗时"
					+ (System.currentTimeMillis() - testTick) + "ms");

			// 数据库中写入新增skyid数
			try {
				MmochatMain.serverListDaoService.addNewDayLog(startTick, sum,
						average, totalLoginSkyIdNum, totalDirectPayRmb,
						directPaySkyIdNum, directPayTimes, totalKBPayRmb,
						KBPaySkyIdNum, KBPayTimes, mixPaySkyIdNum);
			} catch (Exception e) {
				e.printStackTrace();
			}

			System.out.println("数据保存完毕!");
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
}
